//package com.ttedu.action.manage.login;
//
//
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.authz.AuthorizationException;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.cache.Cache;
//import org.apache.shiro.cache.MemoryConstrainedCacheManager;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.subject.SimplePrincipalCollection;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//@Component
//public class BasicSecurityRealm extends AuthorizingRealm{
//	private static Log log = LogFactory.getLog(BasicSecurityRealm.class);
//	@Autowired
//	private UserLoginService userloginservice;
//	public BasicSecurityRealm(){
//		 setName("chinaedu");
//		 setCacheManager(new MemoryConstrainedCacheManager());
//	}
//	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//		if(null==principals)
//			throw new AuthorizationException("PrincipalCollection was null, which should not happen");
//		if (principals.isEmpty())
//            return null;
//        if (principals.fromRealm(getName()).size() <= 0)
//            return null;
//
//        String username = (String) principals.fromRealm(getName()).iterator().next();
//        if (username == null)
//            return null;
//        user suser=userloginservice.findToKeName(username);
//        if (suser == null) return null;
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//		return info;
//	}
//	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)  throws AuthenticationException{
//		UsernamePasswordToken userToken = (UsernamePasswordToken) token;
//        user suser=userloginservice.findToKe(String.valueOf(userToken.getUsername()),String.valueOf(userToken.getPassword()));
//        if (suser == null) {
//            throw new AuthenticationException("The user doesn't exist");
//        } else {
//        	return new SimpleAuthenticationInfo(suser.getUsername(), suser.getPasswd(),getName());
//        }
//	}
//	   /** 
//     * 更新用户授权信息缓存. 
//     */ 
//    public void clearCachedAuthorizationInfo(String principal) { 
//        SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName()); 
//        clearCachedAuthorizationInfo(principals); 
//    } 
//
//    /** 
//     * 清除所有用户授权信息缓存. 
//     */ 
//    public void clearAllCachedAuthorizationInfo() { 
//        Cache<Object, AuthorizationInfo> cache = getAuthorizationCache(); 
//        if (cache != null) { 
//            for (Object key : cache.keys()) { 
//                cache.remove(key); 
//            } 
//        } 
//    } 
//}
